package com.touzhijia.authority.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.touzhijia.authority.domain.dto.request.PermissionAddReq;
import com.touzhijia.authority.domain.dto.request.PermissionQueryByRoleReq;
import com.touzhijia.authority.domain.dto.request.PermissionQueryReq;
import com.touzhijia.authority.domain.dto.request.PermissionUpdateReq;
import com.touzhijia.authority.domain.dto.response.PermissionByRoleRes;
import com.touzhijia.authority.domain.entity.Permission;
import com.touzhijia.authority.exception.BusinessException;
import com.touzhijia.authority.mapper.PermissionMapper;
import com.touzhijia.authority.mapper.RoleMapper;
import com.touzhijia.authority.mapper.WebPermissionMapper;
import com.touzhijia.authority.utils.mapper.BeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

@Transactional(readOnly = true)
@Service
public class PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private WebPermissionMapper webPermissionMapper;
    @Autowired
    private RoleMapper roleMapper;

    public Permission getPermission(String permission) {
        return permissionMapper.getByPermission(permission);
    }

    public PageInfo<Permission> findPermission(PermissionQueryReq queryReq) {
        return PageHelper
                .startPage(queryReq.getPageNo(), queryReq.getPageSize())
                .doSelectPageInfo(() -> permissionMapper.findPermission(queryReq));
    }

    public PageInfo<PermissionByRoleRes> findPermissionByRole(PermissionQueryByRoleReq queryReq) {
        return PageHelper
                .startPage(queryReq.getPageNo(), queryReq.getPageSize())
                .doSelectPageInfo(() -> permissionMapper.findPermissionByRole(queryReq));
    }

    @Transactional
    public Permission addPermission(PermissionAddReq addReq) {
        Permission exists = permissionMapper.getByPermission(addReq.getPermission());
        if (exists != null) {
            throw new BusinessException("权限已存在");
        }
        Permission permission = BeanMapper.mapper(addReq, Permission.class);
        permission.setResourcesType(Permission.Resources_Type_2);
        permission.setCreateAt(new Date());
        permissionMapper.insertSelective(permission);
        return permission;
    }

    @Transactional
    public Permission del(String permission) {
        Permission dbPermission = permissionMapper.getByPermission(permission);
        if (dbPermission == null) {
            throw new BusinessException("权限信息不存在");
        }
        if (Objects.equals(dbPermission.getResourcesType(), Permission.Resources_Type_1)) {
            // 删除 web_permission
            webPermissionMapper.deleteByPermission(permission);
        }
        // 删除 permission
        permissionMapper.deleteByPermission(permission);
        // 删除 role_permission
        roleMapper.deleteByPermission(permission);
        // TODO Spring Security 处理
        return dbPermission;
    }

    @Transactional
    public Permission updatePermission(String permissionStr, PermissionUpdateReq updateReq) {
        Permission permission = permissionMapper.getByPermission(permissionStr);
        if (permission == null) {
            throw new BusinessException("权限不存在");
        }
        if (Objects.equals(permission.getResourcesType(), Permission.Resources_Type_1) && !Objects.equals(permission.getModule(), updateReq.getModule())) {
            throw new BusinessException("当前权限类型不支持更新模块名称");
        }
        if (Objects.equals(permission.getResourcesType(), Permission.Resources_Type_1) && !Objects.equals(permission.getPermission(), updateReq.getPermission())) {
            throw new BusinessException("当前权限类型不支持更新权限字符串");
        }
        if (!Objects.equals(permission.getPermission(), updateReq.getPermission())) {
            Permission tmp = permissionMapper.getByPermission(updateReq.getPermission());
            if (tmp != null && !Objects.equals(tmp.getId(), permission.getId())) {
                throw new BusinessException("当前更新权限字符串已经存在");
            }
        }
        Permission update = BeanMapper.mapper(updateReq, Permission.class);
        update.setId(permission.getId());
        update.setUpdateAt(new Date());
        permissionMapper.updateByPrimaryKeySelective(update);
        return permissionMapper.getByPermission(permissionStr);
    }
}
